
import { HttpRequest, create } from './HttpRequest'

export { default as http } from './axios'

export { HttpRequest, create }
/**
 * key=   对应后端项目名称（例如zongheng-auth，简写成 auth）
 * value= 对应后端项目所在服务API接口地址，如不配置value，默认取 process.env.VUE_APP_API_BASE_URL
 */
export const auth = create(process.env.VUE_APP_API_ZONGHENG_AUTH)
export const application = create(process.env.VUE_APP_API_ZONGHENG_APPLICATION)
export const message = create(process.env.VUE_APP_API_ZONGHENG_MESSAGE)
export const system = create(process.env.VUE_APP_API_ZONGHENG_SYSTEM)
export const yunfeng = create(process.env.VUE_APP_API_ZONGHENG_YUNFENG)
export const feiliu = create(process.env.VUE_APP_API_ZONGHENG_FEILIU)
export const exchanger = create(process.env.VUE_APP_API_ZONGHENG_EXCHANGER)
export const task = create(process.env.VUE_APP_API_ZONGHENG_TASK)
export const storage = create(process.env.VUE_APP_API_ZONGHENG_STORAGE)
export const quartz = create(process.env.VUE_APP_API_ZONGHENG_QUARTZ)
export const adapter = create(process.env.VUE_APP_API_ZONGHENG_ADAPTER)
export const metadata = create(process.env.VUE_APP_API_ZONGHENG_METADATA)
export const connector = create(process.env.VUE_APP_API_ZONGHENG_CONNECTOR)
export const jingran = create(process.env.VUE_APP_API_ZONGHENG_JINGRAN)

storage.uploadURL = `${storage.baseURL}/storage/upload/`
storage.downloadURL = `${storage.baseURL}/storage/download/`
storage.base64DownloadURL = `${storage.baseURL}/storage/downloadbase64/`

storage.downloadByFileId = (fileId) => {
  if (process.env.VUE_APP_ENABLE_BASE64_DOWNLOAD == 'true') {
    return storage.downloadByPath(`${storage.base64DownloadURL}${fileId}`)
  } else {
    return storage.downloadByPath(`${storage.downloadURL}${fileId}`)
  }
}

storage.downloadByPath = (path) => {
  if (process.env.VUE_APP_ENABLE_BASE64_DOWNLOAD == 'true') {
    return new Promise((resolve, reject) => {
      storage.get(path).then(response => {
        resolve(`data:${response.contentType};base64,` + response.base64, response.filename)
      })
    })
  } else {
    return new Promise((resolve, reject) => {
      if (!path.startsWith('http://') && !path.startsWith('https:/')) {
        path = `${storage.baseURL}/${path}`
      }
      resolve(path)
    })
  }
}